using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Mall.Api.Entity;
using Mall.Api.ParamModel;
using Microsoft.IdentityModel.Tokens;

namespace Mall.Api.Utils
{
    public class TokenHelper
    {
        public static string GenerateToekn(TokenParameter tokenParameter, Users user,Supplier supplier)
        {

            var id = user?.Id.ToString();
            var supplierId =supplier?.Id.ToString();
            
            
            var claims = new[]
            {

                new Claim("nickname", user?.Nickname ?? ""),
                new Claim("username", user?.Username??""),
                new Claim("supplierId",supplierId??""),
                new Claim("rolesId", user?.RolesId.ToString()??""),  
                new Claim("id",id??""),
                new Claim("txcolor",user?.Txcolor??""),
                new Claim("bgcolor",user?.Bgcolor??"")
                

            };

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenParameter.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(tokenParameter.Issuer, null, claims, expires: DateTime.UtcNow.AddMinutes(tokenParameter.AccessExpiration), signingCredentials: credentials);

            var token = new JwtSecurityTokenHandler().WriteToken(jwtToken);

            return token;
        }
    }
}